home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Cream of the Crop 26
/
Cream of the Crop 26.iso
/
program
/
ccdl150l.zip
/
IO
/
SPRINTF.C
< prev
next >
Wrap
C/C++ Source or Header
|
1997-03-09
|
6KB
|
297 lines
#include <stdio.h>
#include <ctype.h>
#include <string.h>
/* Floating point not implemented */
#define NUM_SIZE 32
static int getnum(char *string, int num,int radix,int lc,int unsign)
{
int i,sz=0;
unsigned unum;
for (i=0; i < NUM_SIZE-1; i++)
string[i] = '0';
string[NUM_SIZE-1] = 0;
if (num < 0 && !unsign)
unum = - num;
else
unum = num;
i = NUM_SIZE-2;
while (unum) {
string[i] = (char)((unum % radix)+ '0');
if (string[i] > '9') {
string[i] += 7;
if (lc)
string[i] += 32;
}
i--;
unum /= radix;
sz++;
}
if (sz == 0)
sz++;
return sz;
}
static char *onetostr(char *obuf,char **buffer, const char *format, void *arg,int *count)
{
int c,sz;
int *p;
int looping = 1;
int issigned = 0, ljustify = 0, spaced = 0, prefixed = 0;
int leadzero = 0;
int width = 0;
int prec = 6;
int mode = 0;
int lc,i;
char locbuf[NUM_SIZE],*ofm = format;
while (looping)
switch (*format) {
case '+': issigned = 1;
format++;
break;
case '-': ljustify = 1;
format++;
break;
case ' ': spaced = 1;
format++;
break;
case '#': prefixed = 1;
format++;
break;
case '0': leadzero = 1;
format++;
break;
default:
looping = 0;
}
if (isdigit(*format)) {
width = 0;
while (isdigit(*format)) {
width *= 10;
width += *format++ - '0';
}
}
if (*format == '.') {
format++;
prec = 0;
while (isdigit(*format)) {
prec *= 10;
prec += *format++ - '0';
}
}
if (*format == 'h' || *format == 'l' || *format == 'L')
mode = *format++;
switch (*format++) {
case '%':
*(*buffer)++ = '%';
*(*buffer) = 0;
break;
case 'c':
(*count)++;
c = *(int *)arg;
*(*buffer)++ = (char)c;
*(*buffer) = 0;
break;
case 'd':
case 'i':
(*count)++;
c = *(int *)arg;
if (mode == 'h') {
c &= 0xffff;
if (c & 0x8000)
c |= 0xffff0000;
}
sz = getnum(locbuf,c,10,0,0);
if (issigned) {
if (c < 0)
*(*buffer)++ = '-';
else
*(*buffer)++ = '+';
if (width)
width--;
}
else if (spaced) {
if (c < 0)
*(*buffer)++ = '-';
else
*(*buffer)++ = ' ';
if (width)
width--;
}
else {
if (c < 0) {
*(*buffer)++ = '-';
if (width)
width--;
}
}
goto numfin;
case 'e':
case 'E':
case 'f':
case 'g':
case 'G':
#ifndef USE_FLOAT
(*count)++;
if (mode == 'L')
(*count)++;
strcpy(*buffer,"FP not linked");
*buffer += strlen(*buffer);
#endif
break;
case 'n':
(*count)++;
p = *(int **)arg;
*p = (int)((*buffer) - obuf);
break;
case 'o':
(*count)++;
c = *(int *)arg;
if (mode == 'h') {
c &= 0xffff;
if (c & 0x8000)
c |= 0xffff0000;
}
sz = getnum(locbuf,c,8,0,1);
if (prefixed && c)
sz--;
if (issigned) {
*(*buffer)++ = '+';
if (width)
width--;
}
else if (spaced) {
*(*buffer)++ = ' ';
if (width)
width--;
}
goto numfin;
case 's':
(*count)++;
sz = strlen(*(char **)arg);
if (width && (sz < width))
if (ljustify) {
for (i=0; i < width-sz; i++)
*(*buffer)++ = ' ';
strcpy(*buffer,*(char **)arg);
*buffer += strlen(*buffer);
}
else {
strcpy(*buffer,*(char **)arg);
*buffer += strlen(*buffer);
for (i=0; i < width-sz; i++)
*(*buffer)++ = ' ';
}
else {
strcpy(*buffer,*(char **)arg);
*buffer += strlen(*buffer);
}
break;
case 'u':
(*count)++;
c = (int )arg;
if (mode == 'h') {
c &= 0xffff;
if (c & 0x8000)
c |= 0xffff0000;
}
sz = getnum(locbuf,*(int *)arg,10,0,1);
if (issigned) {
*(*buffer)++ = '+';
if (width)
width--;
}
else if (spaced) {
*(*buffer)++ = ' ';
if (width)
width--;
}
goto numfin;
case 'x':
case 'p':
lc = 1;
case 'X':
(*count)++;
if (*(format-1) == 'X')
lc = 0;
c = *(int *)arg;
if (mode == 'h') {
c &= 0xffff;
if (c & 0x8000)
c |= 0xffff0000;
}
sz = getnum(locbuf,c,16,lc,1);
if (issigned) {
*(*buffer)++ = '+';
if (width)
width--;
}
else if (spaced) {
*(*buffer)++ = ' ';
if (width)
width--;
}
if (prefixed) {
*(*buffer)++ = '0';
*(*buffer)++ = 'x';
}
numfin:
if (width) {
if (width <= sz) {
goto nowidth;
}
if (!ljustify || leadzero) {
if (leadzero) {
for (i=0; i < width-sz; i++)
*(*buffer)++ = '0';
}
else {
for (i=0; i < width-sz; i++)
*(*buffer)++ = ' ';
}
strcpy(*buffer,&locbuf[NUM_SIZE-sz-1]);
(*buffer) += strlen(*buffer);
}
else {
strcpy(*buffer,&locbuf[NUM_SIZE-sz-1]);
(*buffer) += strlen(*buffer);
for (i=0; i < width-sz; i++)
*(*buffer)++ = ' ';
*(*buffer) = 0;
}
}
else {
nowidth:
strcpy(*buffer,&locbuf[NUM_SIZE-sz-1]);
(*buffer) += strlen(*buffer);
*(*buffer) = 0;
}
break;
default:
*(*buffer)++ = '%';
format = ofm;
break;
}
return(format);
}
int vsprintf(char *buffer, const char *format,
void *arglist)
{
int i = 0;
char *obuf = buffer;
while (*format) {
while (*format != '%' && *format != 0)
*buffer++ = *format++;
if (*format && *(format+1))
format = onetostr(obuf,&buffer,format+1,&((char **)arglist+i),&i);
}
*buffer++ = 0;
return(strlen(obuf));
}
int sprintf(char *buffer, const char *format, ...)
{
return vsprintf(buffer,format,(((char *)&format) + sizeof(char *)));
}